<?php
/**
 * Represents a listbox.
 */
fily_XMLConverter::registerNamespace('listbox', fily_ListBox);
class fily_ListBox extends fily_BasicList {
    /**
     *
     */
    public function __construct() {
        parent::__construct('select', 'option');
    }

    /**
     * adds a element to the list
     * @param string $value
     * @param string $text
     * @param boolean $selected default is false
     * @return fily_Control
     */
    public function addElement($value, $text,$selected = false) {
        if($selected) {
            // remove selected attributes at all items
            foreach($this->controls->getItems() as $fily_Control) {
                if($fily_Control instanceof fily_Control && $fily_Control->getTag()=='option') {
                    $fily_Control->attributes->remove('selected');
                }
            }
        }
        $option =  $this->addItem($text);
        $option->attributes->set('value', $value);
        if($selected){
            $option->attributes->set('selected', 'selected');
        }
        return $option;
    }

    /**
     * selects an element by the value
     * @param string $value
     */
    public function selectByValue($value) {
        $found = false;
        foreach($this->controls->getItems() as $control) {
            if($control instanceof fily_Control && $control->getTag()=='option') {

                // if not already found and the value attribute is equals the searched value
                if(!$found && $control->attributes->get('value')==$value) {
                    $control->attributes->set('selected', 'selected');
                    $found = true;
                }
                else {
                    $control->attributes->remove('selected');
                }
            }
        }
    }

    /**
     * selects an element by the text
     * @param string $text
     */
    public function selectByText($text) {
        $found = false;
        foreach($this->controls->getItems() as $control) {
            if($control instanceof fily_Control&& $control->getTag()=='option') {
                // if not already found and the first fily_Control is a text object and the searched text
                if(!$found && $control->controls->get(0)!=null
                        && $control->controls->get(0) instanceof fily_Text
                        && $control->controls->get(0)->getText()==$text) {
                    $control->attributes->set('selected', 'selected');
                    $found = true;
                }
                else {
                    $control->attributes->remove('selected');
                }
            }
        }
    }

    /**
     *  unselect all items
     */
    public function unselect() {
        foreach($this->controls->getItems() as $control) {
            if($control instanceof fily_Control&& $control->getTag()=='option') {
                $control->attributes->remove('selected');
            }
        }
    }

    /**
     * returns the selected value or null
     * @return string
     */
    public function getSelectedValue() {
        $value = null;
        foreach($this->controls->getItems() as $control) {
            if($control instanceof fily_Control && $control->getTag()=='option') {
                if($control->attributes->get('selected')=='selected') {
                    $value = $control->attributes->get('value');
                    break;
                }

            }
        }
        return $value;
    }

    /**
     * returns the selected text or null
     * @return string
     */
    public function getSelectedText() {
        $text = null;
        foreach($this->controls->getItems() as $control) {
            if($control instanceof fily_Control && $control->getTag()=='option') {
                if($control->attributes->get('selected')=='selected') {
                    if($control->controls->get(0) instanceof fily_Text) {
                        $text = $control->controls->get(0)->getText();
                    }
                    break;
                }

            }
        }
        return $text;
    }

    public function XMLCallFunction($name, array $parameters, $innerValue, fily_ControlToHtml $innerControl = null){
        switch ($name) {
            case 'addelement':
                if(isset($parameters['text']))
                     $text = $parameters['text'];
                else
                     $text = $innerValue;
                
                if(isset($parameters['value']))
                    $value = $parameters['value'];
                else
                     $value = $text;

                if(isset($parameters['selected']))
                    $selected= true;
                else
                    $selected= false;
                
                $this->addElement($value, $text, $selected);
                break;
            default:
                parent::XMLCallFunction($name, $parameters, $innerValue);
                break;
        }
    }
    

}

?>
